<?php
	
	/**
	 * 字符串转变为double型.
	 * @param  [type] $val [description]
	 * @return [type]      [description]
	 */
	function double_format($val){
		return $val>0 ? sprintf("%.2f",$val) : $val<0 ? sprintf("%.2f",$val) : 0;
    }

	/**
	 * 计算星期天.
	 * @param  [type] $num [description]
	 * @return [type]      [description]
	 */
	function get_week_day($num){
	$s	=	'';
	switch ($num) {
    	case 0:	
			$s	=	'天';
			break;
    	case 1:	
			$s	=	'一';
			break;
    	case 2:	
			$s	=	'二';
			break;
    	case 3:	
			$s	=	'三';
			break;
    	case 4:	
			$s	=	'四';
			break;
    	case 5:	
			$s	=	'五';
			break;
    	case 6:	
			$s	=	'六';
			break;
	}
	return $s;
}

	function add_days($days){
		$H=date(H);
		$i=date(i);
		$s=date(s);

		$Y=date(Y);
		$m=date(m);
		$d=date(d);

		$out_date=date( "Y-m-d H:i:s", mktime($H,$i,$s,$m,$d+$days,$Y) );

		return $out_date;
	}

	function add_months($months){
		$H=date(H);
		$i=date(i);
		$s=date(s);

		$Y=date(Y);
		$m=date(m);
		$d=date(d);

		$out_date=date( "Y-m-d H:i:s", mktime($H,$i,$s,$m+$months,$d,$Y) );

		return $out_date;
	}

	function add_years($years){
		$H=date(H);
		$i=date(i);
		$s=date(s);

		$Y=date(Y);
		$m=date(m);
		$d=date(d);

		$out_date=date( "Y-m-d H:i:s", mktime($H,$i,$s,$m,$d,$Y+$years) ); 
		return $out_date;
	}

	function add_years_to_date($dateStr,$years){		
		$newDate = date("Y-m-d H:i:s",strtotime("$dateStr +1 year"));
		return $newDate;
	}


	function compare_dates($dateStr1, $dateStr2){
		$date1=strtotime($dateStr1); 	
		$date2=strtotime($dateStr2); 	

		if($date1 >= $date2)
			return true;
		else
			return false;
	}

	/**
	 * 根据数字, 在左边补零.
	 * @param  [type] $num    [description]
	 * @param  [type] $length [description]
	 * @return [type]         [description]
	 */
	function bu_ling($num, $length){
		return str_pad($num, $length, '0', STR_PAD_LEFT);	
	}
	

	/**
	 * 得到IP地址.
	 */
	function getip()
	{
	    if(!empty($_SERVER["HTTP_CLIENT_IP"]))
	    {
	        $cip = $_SERVER["HTTP_CLIENT_IP"];
	    }
	    else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
	    {
	        $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	    }
	    else if(!empty($_SERVER["REMOTE_ADDR"]))
	    {
	        $cip = $_SERVER["REMOTE_ADDR"];
	    }
	    else
	    {
	        $cip = '';
	    }

	    preg_match("/[\d\.]{7,15}/", $cip, $cips);
	    $cip = isset($cips[0]) ? $cips[0] : 'unknown';
	    unset($cips);
	    return $cip;
	}